package lucene;

import java.io.File;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;

import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

public class DocumentBoost {
	public static void main(String[] args) throws Exception {
		DocumentBoost d = new DocumentBoost();
		d.testNormsDocBoost();
	}
	
	public void testNormsDocBoost() throws Exception {
		File indexDir = new File("../mydemo/resource/index/testNormsDocBoost");
		IndexWriter writer = new IndexWriter(FSDirectory.open(indexDir), new
		StandardAnalyzer(Version.LUCENE_CURRENT), true, IndexWriter.MaxFieldLength.LIMITED);
		writer.setUseCompoundFile(false);
		Document doc1 = new Document();
		Field f1 = new Field("contents", "common hello hello", Field.Store.NO, Field.Index.ANALYZED);
		doc1.add(f1);
		doc1.setBoost(100);
		writer.addDocument(doc1);
		Document doc2 = new Document();
		Field f2 = new Field("contents", "common common hello", Field.Store.NO,
		Field.Index.ANALYZED_NO_NORMS);
		doc2.add(f2);
		writer.addDocument(doc2);
		Document doc3 = new Document();
		Field f3 = new Field("contents", "common common common", Field.Store.NO,
		Field.Index.ANALYZED_NO_NORMS);
		doc3.add(f3);
		writer.addDocument(doc3);
		writer.close();
		IndexReader reader = IndexReader.open(FSDirectory.open(indexDir));
		IndexSearcher searcher = new IndexSearcher(reader);
		TopDocs docs = searcher.search(new TermQuery(new Term("contents", "common")), 10);
		for (ScoreDoc doc : docs.scoreDocs) {
		System.out.println("docid : " + doc.doc + " score : " + doc.score);
		}
	}
}
